/*
Reversi.
Copyright (C) <2008>  <Vladimir Simo, Kristian Valentin, Michal Vrabel, Tibor Balazs>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//#pragma once

//#include <iostream>
#include "hrac.h"

using namespace std;

class UserHrac:public Hrac {
    private:
    	    void setGameField(typPole &pole1);
    public: 
	        UserHrac(int color);
         	void move(typPole &pole1, int x, int y); 
          	bool checkMove(typPole &pole1, int x, int y);
           	int getNumOfPossibleMoves(typPole& pole1, int color);
                Coordinates *getMoves(typPole& pole1, int color) {return getPossibleMoves(pole1, color);}
};

UserHrac::UserHrac(int color):Hrac() {
    setColor(color);
}

void UserHrac::setGameField(typPole &pole1) {
    for (int i = 0; i < rozmerPola; i++) {
	for (int j = 0; j < rozmerPola; j++) {
	    pole[i][j] = pole1[i][j];
	    root->pole[i][j] = pole[i][j];
	}
    }
}

void UserHrac::move(typPole &pole1, int x, int y) {
    Coordinates coor;
    
    coor.setX(x);
    coor.setY(y);
    
    printf("\nx = %d\ny = %d\n", coor.getX(), coor.getY());
    
    makeMove(&coor, pole1, getColor());
}

bool UserHrac::checkMove(typPole &pole1, int x, int y) {
    //Coordinates *tmp = this->getMoves(pole1, this->getColor());
    Coordinates *tmp = this->getPossibleMoves(pole1, this->getColor());
    bool allow_move = false;

    //ak dane pole patri do skupiny moznych tahov
    while (tmp != NULL) {
        if ((tmp->getX() == x) && (tmp->getY() == y)) {
                allow_move = true;
        }
        tmp = tmp->getNext();
    }

    //zrus vytvoreny zoznam pre koordinaty
    if (tmp != NULL) {
            tmp->freeCoordinates();
    }

    //ak je dane policko aj prazdne
    if (allow_move && (pole1[x][y] == prazdne) && (x != rozmerPola) && (y != rozmerPola)) {
        return true;
    }
    else {
        return false;
    }
}

int UserHrac::getNumOfPossibleMoves(typPole& pole1, int color) {
    Coordinates *tmp = getPossibleMoves(pole1, color);

    int counter = 0;
    while(tmp != NULL) {
        tmp = tmp->getNext();
        counter ++;
    }

    //zrus zoznam koordinatov
    if (tmp != NULL) {
            tmp->freeCoordinates();
    }
    
    return counter;
}

